import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn } from '@delon/abc';
import { applyCC } from '@shared';
import { _HttpClient, ModalHelper, SettingsService } from '@delon/theme';
import { NzNotificationService } from 'ng-zorro-antd';
import { ProductYearEditComponent } from './edit/product-year-edit.component';
import { zip } from 'rxjs';
import { catchError } from 'rxjs/operators';

/**
 * 产品季配置
 */
@Component({
  selector: 'app-productyear',
  templateUrl: './product-year.component.html',
  styleUrls: ['./product-year.component.less'],
})
export class ProductYearComponent implements OnInit {
  auth_btn_add = false;
  auth_btn_edit = false;
  auth_btn_del = false;
  url = `/cf/prod/sys-prod/prodyear/page`;
  params = { key: '' };
  brands = [];
  brandTypes = [];
  years = [];

  @ViewChild('st', { static: false }) st: STComponent;
  columns: STColumn[];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private notice: NzNotificationService,
    private settings: SettingsService,
  ) {
    this.columns = this.buildCol();
  }

  ngOnInit() {
    this.auth_btn_add = this.settings.user.permissions.prodyear_add_btn;
    this.auth_btn_edit = this.settings.user.permissions.prodyear_edit_btn;
    this.auth_btn_del = this.settings.user.permissions.prodyear_del_btn;

    for (let i = new Date().getFullYear() + 1; i >= 2012; i--) {
      this.years.push(i + '');
    }
    const api = [this.http.get(`/cf/prod/sys-prod/brands`)];
    zip(...api)
      .pipe(
        catchError(([brands]) => {
          return [brands];
        }),
      )
      .subscribe(([brands]) => {
        this.brands = brands.data.map(b => ({ text: b, value: b }));
        this.st.resetColumns({ columns: this.buildCol() });
      });
  }

  buildCol(): STColumn[] {
    return [
      {
        title: '品牌',
        index: 'brand',
        sort: true,
        filter: {
          menus: this.brands,
          key: 'brands',
        },
      },
      // {
      //   title: '品类',
      //   index: 'brandType',
      //   sort: true,
      //   filter: {
      //     menus: this.brandTypes,
      //     key: 'brandTypes',
      //   },
      // },
      { title: '年份', index: 'year', sort: true },
      { title: '起始时间', index: 'startTime', type: 'date', sort: true },
      { title: '结束时间', index: 'endTime', type: 'date', sort: true },
      {
        ccDisabled: true,
        title: '操作',
        buttons: [
          {
            text: '编辑',
            type: 'static',
            modal: { component: ProductYearEditComponent, paramsName: 'record' },
            click: 'reload',
            iif: (item: STColumn) => {
              return this.auth_btn_edit;
            },
          },
          {
            text: '删除',
            type: 'del',
            pop: {
              title: '确定删除配置？',
              okType: 'danger',
            },
            click: (record, _modal, comp) => {
              this.http.delete(`/cf/prod/sys-prod/prodyear/${record.id}`).subscribe(res => {
                if (res.status === 200) {
                  this.notice.success('成功', `成功删除`);
                  comp!.load();
                }
              });
            },
            iif: (item: STColumn) => {
              return this.auth_btn_del;
            },
          },
        ],
      },
    ];
  }

  add() {
    this.modal.createStatic(ProductYearEditComponent, { i: { id: 0 } }).subscribe(() => this.st.reload());
  }
}
